using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LunoCode.Render
{
	class SkyDome : BaseObject
	{
		private Model model;
		private Texture2D texture;

		public SkyDome() : this(new Vector3(0, -100000, 0), new Vector3(-320000, 320000, -320000))
		{

		}

		public SkyDome(Vector3 position, Vector3 scale)
		{
			this.Scale = scale;
			this.Position = position;
		}

		public void LoadSkyDome(GraphicsDevice device, Effect effect, Model skyModel)
		{
			model = skyModel;

			foreach (ModelMesh mesh in model.Meshes)
				foreach (BasicEffect currenteffect in mesh.Effects)
					texture = currenteffect.Texture;

			foreach (ModelMesh modmesh in model.Meshes)
				foreach (ModelMeshPart modmeshpart in modmesh.MeshParts)
					modmeshpart.Effect = effect.Clone(device);
		}

		#region Draw
		public void Draw(Matrix view, Matrix proj)
		{
			foreach (ModelMesh modmesh in model.Meshes)
			{
				foreach (Effect currenteffect in modmesh.Effects)
				{
					currenteffect.CurrentTechnique = currenteffect.Techniques[0];

					currenteffect.Parameters["WvpXf"].SetValue(World * view * proj);
					currenteffect.Parameters["WorldXf"].SetValue(World);
					Matrix wIT = Matrix.CreateScale(Scale)
										 * Matrix.CreateRotationX(-MathHelper.PiOver2)
										 * RotationMatrix
										 * Matrix.Invert(Matrix.CreateTranslation(Position));
					currenteffect.Parameters["WorldITXf"].SetValue(wIT);
					currenteffect.Parameters["ViewIXf"].SetValue(Matrix.Invert(view));
					currenteffect.Parameters["ColorTexture"].SetValue(texture);

					currenteffect.Parameters["Ks"].SetValue(0);
				}
				modmesh.Draw();
			}
		}

		#endregion

	}
}